﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Data;
using PayRoller.Code;
using HySoft.Cash;

namespace PayRoller.Include
{
    public partial class UInvoiceDisplay : System.Web.UI.UserControl
    {
        public int OrgID { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack == false)
            {
                this.DisplaySettingsStatus();
                this.BindInfo();
            }
        }

        public void BindInfo()
        {
            if (OrgID > 0)
            {
                this.BindClientInfo();
                this.BindInvoiceInfo();
            }
        }

        private void BindClientInfo()
        {
            using (TorginfoBN obn = new TorginfoBN(this.Page))
            {
                TorginfoDT detail = obn.Get(this.OrgID);
                if (detail != null)
                {
                    this.labClientName.Text = detail.OrgName;
                    this.labOurReference.Text = detail.Sid.ToString();
                }
            }
        }

        private void BindInvoiceInfo()
        {
            using (TinvoicelistBN obn2 = new TinvoicelistBN(this.Page))
            {
                //需要根据当前企业ID获取其下的Invoice列表
                obn2.QueryOrgSid(this.OrgID);
                DataTable table = obn2.GetList();
                this.dlInvoiceList.DataSource = table;
                this.dlInvoiceList.DataBind();
            }
        }

        /// <summary>
        /// 本方法设置了 需要显示隐藏的部分配置项；其他的配置项在 DataList的绑定事件中设置。
        /// </summary>
        private void DisplaySettingsStatus()
        {
            ReportSettingsInvoiceDT setting = GetReportSettings();
            if (setting != null)
            {
                this.trClientName.Visible = setting.IsDisplayOrgName;
                this.trOurReference.Visible = setting.IsDisplaySystemReference;
            }
        }

        private ReportSettingsInvoiceDT GetReportSettings()
        {
            ReportSettingsBN business = new ReportSettingsBN(this.Page);
            ReportSettingsInvoiceDT setting = business.GetReportSettingsInvoice(0);
            return setting;
        }

        protected void dlInvoiceList_ItemCreated(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            ReportSettingsInvoiceDT setting = GetReportSettings();
            switch (itemType)
            {
                case ListItemType.Header:
                    e.Item.FindControl("thDateOfInvoice").Visible = setting.IsDisplayDateOfInvoice;
                    e.Item.FindControl("thInvoiceDueDate").Visible = setting.IsDisplayInvoiceDueDate;
                    e.Item.FindControl("thNetPay").Visible = setting.IsDisplayNetPay;
                    e.Item.FindControl("thFinanceCost").Visible = setting.IsDisplayFinanceCost;
                    e.Item.FindControl("thAmountCharged").Visible = setting.IsDisplayAmountCharged;
                    e.Item.FindControl("thPenalty").Visible = setting.IsDisplayPenalty;
                    e.Item.FindControl("thLateInterest").Visible = setting.IsDisplayLateInterest;
                    e.Item.FindControl("thAmountReceived").Visible = setting.IsDisplayAmountReceived;
                    e.Item.FindControl("thBalance").Visible = setting.IsDisplayBalance;
                    e.Item.FindControl("thStatusOfInvoice").Visible = setting.IsDisplayStatusOfInvoice;
                    break;
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.SelectedItem:
                    e.Item.FindControl("tdDateOfInvoice").Visible = setting.IsDisplayDateOfInvoice;
                    e.Item.FindControl("tdInvoiceDueDate").Visible = setting.IsDisplayInvoiceDueDate;
                    e.Item.FindControl("tdNetPay").Visible = setting.IsDisplayNetPay;
                    e.Item.FindControl("tdFinanceCost").Visible = setting.IsDisplayFinanceCost;
                    e.Item.FindControl("tdAmountCharged").Visible = setting.IsDisplayAmountCharged;
                    e.Item.FindControl("tdPenalty").Visible = setting.IsDisplayPenalty;
                    e.Item.FindControl("tdLateInterest").Visible = setting.IsDisplayLateInterest;
                    e.Item.FindControl("tdAmountReceived").Visible = setting.IsDisplayAmountReceived;
                    e.Item.FindControl("tdBalance").Visible = setting.IsDisplayBalance;
                    e.Item.FindControl("tdStatusOfInvoice").Visible = setting.IsDisplayStatusOfInvoice;
                    break;
            }
        }

        protected void dlInvoiceList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if (itemType == ListItemType.SelectedItem || itemType == ListItemType.Item || itemType == ListItemType.AlternatingItem)
            {
                DataRowView drv = e.Item.DataItem as DataRowView;
                Label labDateOfInvoice = e.Item.FindControl("labDateOfInvoice") as Label;
                if(labDateOfInvoice!=null)
                {
                    labDateOfInvoice.Text = Convert.ToDateTime(drv["InvoiceDate"]).ToString(AppSettings.CurrentCulture);
                }

                Label labInvoiceDueDate= e.Item.FindControl("labInvoiceDueDate") as Label;
                if (labInvoiceDueDate != null)
                {
                    labInvoiceDueDate.Text = Convert.ToDateTime(drv["InvoiceDate"]).ToString(AppSettings.CurrentCulture);
                }

                Label labNetPay= e.Item.FindControl("labNetPay") as Label;
                if (labNetPay != null)
                {
                    labNetPay.Text = Convert.ToString(drv["InvoiceAmount"]);
                }

                Label labPenalty = e.Item.FindControl("labPenalty") as Label;
                if (labPenalty != null)
                {
                    labPenalty.Text = Convert.ToString(drv["Penalty"]);
                }

                Label labLateInterest = e.Item.FindControl("labLateInterest") as Label;
                if (labLateInterest != null)
                {
                    labLateInterest.Text = Convert.ToString(drv["InterestCharges"]);
                }

                Label labAmountReceived = e.Item.FindControl("labAmountReceived") as Label;
                if (labAmountReceived != null)
                {
                    labAmountReceived.Text = Convert.ToString(drv["ReceiveAmount"]);
                }

                Label labBalance = e.Item.FindControl("labBalance") as Label;
                if (labBalance != null)
                {
                    labBalance.Text = Convert.ToString(drv["InvoiceAmount"]);
                }

                //Label labFinanceCost = e.Item.FindControl("labFinanceCost") as Label;
                //if (labFinanceCost != null)
                //{
                //    labFinanceCost.Text = Convert.ToString(drv[""]);
                //}

                //Label labAmountCharged = e.Item.FindControl("labAmountCharged") as Label;
                //if (labInvoiceDueDate != null)
                //{
                //    labInvoiceDueDate.Text = Convert.ToString(drv[""]);
                //}

                //Label labStatusOfInvoice = e.Item.FindControl("labStatusOfInvoice") as Label;
                //if (labInvoiceDueDate != null)
                //{
                //    labInvoiceDueDate.Text = Convert.ToString(drv[""]);
                //}
            }
        }
    }
}